package cn.javass.chapter2.web.controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DATA {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println(getAllUser());
	}
	/* 获取数据库连接的函数*/  
    public static Connection getConnection() {  
        Connection con = null;  //创建用于连接数据库的Connection对象  
        try {  
            Class.forName("com.mysql.jdbc.Driver");// 加载Mysql数据驱动  
              
            con = DriverManager.getConnection(  
                    "jdbc:mysql://localhost:3306/qd", "root", "123456");// 创建数据连接  
              
        } catch (Exception e) {  
            System.out.println("数据库连接失败" + e.getMessage());  
        }  
        return con; //返回所建立的数据库连接  
    } 
    
    public static String  getAllUser() {
		Connection connection = getConnection();
		String sql = "select * from qdzs_user";
		StringBuffer sBuffer = new StringBuffer();
		try {
			Statement statement = connection.createStatement();
			ResultSet resultSet = statement.executeQuery(sql);
			while (resultSet.next()) {
				sBuffer.append("id-"+resultSet.getInt("id"));
				sBuffer.append(" uname-"+resultSet.getString("uname")+"\n");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return sBuffer.toString();
	}

}
